﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using Logic;
using EC_DataBase;
using System.Data;

namespace EasyCheque.Cheque
{
    public partial class DrawCheque : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            Panel_Calendar.Style["display"] = "none";
            Panel_Calendar.Style["position"] = "absolute";

            TextBox_DateOfDraft.Text = DateTime.Now.ToString("yyyy-MM-dd");
            TextBox_DEADLINE.Text = DateTime.Now.AddDays(30).ToString("yyyy-MM-dd");
            TextBox_Amount.Text = "100.00";

            // 先读入所有的AccountID。
            string[] allAccountIDs = OperateDB.Get_All_Acc_IDs(Convert.ToInt32(Session["uid"]));
            foreach (string accountID in allAccountIDs)
            {
                DropDownList_Account.Items.Add(accountID);
            }
        }

        protected void Calendar_InPanel_SelectionChanged(object sender, EventArgs e)
        {
            TextBox_DEADLINE.Text = Calendar_InPanel.SelectedDate.ToString("yyyy-MM-dd");
            Panel_Calendar.Style["display"] = "none";
        }

        protected void Calendar_InPanel_VisibleMonthChanged(Object sender, MonthChangedEventArgs e)
        {
            Panel_Calendar.Style["display"] = "";
        }

        protected void CheckBox_OrderCheque_OnCheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox_OrderCheque.Checked)
            {
                TableRow_RemitteeAccount.Visible = true;
            }
            else
            {
                TableRow_RemitteeAccount.Visible = false;
            }
        }

        protected void CheckBox_NeedPayPassword_OnCheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox_NeedPayPassword.Checked)
            {
                TableRow_PayPassword.Visible = true;
                TableRow_PayPasswordConfirm.Visible = true;
            }
            else
            {
                TableRow_PayPassword.Visible = false;
                TableRow_PayPasswordConfirm.Visible = false;
            }
        }

        protected void Button_Commit_Click(object sender, EventArgs e)
        {
            Stream fs1;

            if (this.FileUpload_cert.HasFile)
            {
                Logic.Logic.LinkDataBase(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(@"\App_Data\EasyCheque.accdb"));
                Logic.Logic.m_ExceptionHandle.AlarmOff();
                fs1 = FileUpload_cert.FileContent;
                string CerNum = Logic.Logic.LogIn(fs1, this.TextBox_PASSWORD.Text);
                XmlDocument XD = new XmlDocument();
                XD.LoadXml(CerNum);
                if (!(XD.DocumentElement.GetAttribute("Result") == "Success"))
                {
                    Label_CommitMessage.Visible = true;
                    return;
                }
                fs1.Position = 0;

                if (CheckBox_OrderCheque.Checked)
                {
	                if (Logic.Logic.QueryAccount(TextBox_RemitteeAccount.Text) == "")
	                {
	                    Label_CommitMessage.Text = "收票账户不存在";
	                    Label_CommitMessage.Visible = true;
	                    return;
	                }
                }

                string CmdStr = @"SELECT [MONEY] FROM ACCOUNT_INFO WHERE ACCOUNT_ID='" + DropDownList_Account.SelectedValue.ToString()  + "'";
                DataSet DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 1)
                {
                    Label_CommitMessage.Text = "数据库查询失败";
                    Label_CommitMessage.Visible = true;
                    return;
                }
                if (Convert.ToDouble(DS.Tables[0].Rows[0][0].ToString()) < Convert.ToDouble(TextBox_Amount.Text))
                {
                    Label_CommitMessage.Text = "账号余额不足";
                    Label_CommitMessage.Visible = true;
                    return;
                }

                string chequenum = "";
                Logic.Certificate cer = new Logic.Certificate();
                cer.LoadCertificate(fs1, "");
                Logic.Logic.GetChequeBase(Server.MapPath(@"\ChequeBase.jpg"));
                MemoryStream MSGet = (MemoryStream)Logic.Logic.MakeCheque(ref chequenum, DropDownList_Account.SelectedValue.ToString(), TextBox_RemitteeAccount.Text, TextBox_Amount.Text, TextBox_DEADLINE.Text, CheckBox_OrderCheque.Checked, CheckBox_BankersCheque.Checked, TextBox_Comment.Text, cer.GetPrivateKey(), TextBox_PayPassword.Text);
                //MemoryStream MSGet = (MemoryStream)Logic.Logic.MakeCheque(ref chequenum, "1234567890123456789", "1234567890123456789", "200.00", "2010/10/10", "Order", "FALSE", "TEST", cer.GetPrivateKey(), "");
                if (chequenum == "")
                {
                    Label_CommitMessage.Visible = true;
                    return;
                }

                FileStream fs = new FileStream(Server.MapPath("/ChequeFile/" + chequenum + ".jpg"), FileMode.Create);
                MSGet.WriteTo(fs);
                fs.Close();
                EC_DataBase.OperateDB.Insert_into_SafeDown(Convert.ToInt32(Session["uid"].ToString()), "ChequeFile/" + chequenum + ".jpg");
                Response.Redirect("~/SafeDown.aspx?filename=ChequeFile/" + chequenum + ".jpg");
            }


        }
    }
}
